<?php
	include ("gen.php");
	$cmd = get_datan("cmd");
	
	switch($cmd){
		case 1:
			d_search();
		break;
		case 2:
			d_delete();
		break;
		case 3:
			d_insert();
		break;
		case 4:
			d_update();
		break;
		case 5:
			d_list();
		break;
		default:
			echo "{";
			echo jsonn("result",0). ",";
			echo jsons("message","unknown command");
			echo "}";
	}
	
	function d_search(){
		include_once("district_functions.php");
			$id = get_datan("district_id");
			$d = new district();
			$d -> search_district($id);
			$row = $d -> fetch();

				
			if(!$row){
			echo "{";
			echo jsonn("result",0).",";
			echo jsons("message","district not found");
			echo "}";
			return;
			}

		echo "{";
			echo jsonn("result", 1).",";
			echo '"district":{';
				echo jsonn("id",$row['district_id']).",";
				echo jsons("districtName",$row['district_name']).",";
				echo jsons("districtCapital",$row['district_capital']).",";
				echo jsonn("regionId",$row['region_id']);
			echo "}";
		echo "}";
		}
	
	function d_delete(){
		include_once("district_functions.php");
		$id = get_datan("district_id");
		$d = new district();
			$row = $d -> delete_row($id);
			

			if(!$row){
				echo "not found";
				exit();
			}
			echo "{";
				echo jsonn("result",2) .",";
				echo '"district":{';	
				echo jsons("district_name", $row['district_name']);
				echo "}";
			echo "}";
	}
	
	function d_insert(){
		include_once("district_functions.php");
		$id = get_datan("d_id");
		$name = get_data("d_name");
		$capital = get_data("d_capital");
		$region_id = get_datan("r_id");
		$d = new district();

		if(!$d -> insert_district($id, $name, $capital, $region_id)){
				echo"{";
					echo jsonn("result", 0) . ",";
					echo jsons("message","error adding district");
				echo"}";
				return;
			}
			echo "{";
				echo jsonn("result",3) . ",";
				echo jsons("message","district added");
			echo "}";
	}
	
	function d_update(){
		include_once("district_functions.php");
			$id = get_datan("d_id");
			$name = get_data("d_name");
			$capital = get_data("d_capital");
			$region_id = get_datan("r_id");
			$d = new district();


			if(!$d -> update_district($id, $name, $capital, $region_id)){
				echo"{";
					echo jsonn("result", 4) . ",";
					echo jsons("message","error updating district");
				echo"}";
				return;
			}
			echo "{";
				echo jsonn("result",0) . ",";
				echo jsons("message","district updated");
			echo "}";
	}
	
	function d_list(){
		include_once("district_functions.php");
			$id = get_datan("district_id");
			$d = new district();
			$row = $d -> list_all();
			
			if(!$row){
				echo "not found";
				exit();
			}
			echo "{";
			echo jsonn("result",5) .",";
			echo '"district":[';
			$row = $d -> fetch();
			while($row){
				

			echo "{";
				echo jsonn("districtId", $row['district_id']) . ",";
				echo jsons("districtName", $row['district_name']) . ",";
				echo jsons("districtCapital", $row['district_capital']) . ",";
				echo jsonn("regionId", $row['region_id']);
			if($row = $d -> fetch()){
				echo "},";
			}else{
				echo "}";
			}
			}
	
		echo "]";
		echo "}";
	}
	
	?>